package pl.mcnova.view;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import pl.mcnova.controller.utils.PrefferedFrameSize;
import pl.mcnova.model.consts.Tabs;
import pl.mcnova.model.dao.ContractorDAO;
import pl.mcnova.view.panels.BottomPanel;
import pl.mcnova.view.panels.DevicesMainPanel;
import pl.mcnova.view.panels.MailPanel;
import pl.mcnova.view.panels.MainPanel;

public class MainView extends JFrame {
	private static PrefferedFrameSize frameSize = new PrefferedFrameSize();
	private MainPanel all;
	private MainPanel projectants;
	private MainPanel architects;
	private MainPanel services;
	private BottomPanel buttonPanel;
	private TabbedPane tabbedPane;
	private DevicesMainPanel devices;
	private MailPanel mails;
	private ContractorDAO contractorDAO;

	public MainView() {
		setTitle("McNova");
		setSize(frameSize.getSize());
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		contractorDAO = new ContractorDAO();

		tabbedPane = new TabbedPane();
		tabbedPane.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {

				all.clearSelection();
				architects.clearSelection();
				projectants.clearSelection();
				services.clearSelection();
				devices.clearSelection();
				mails.clearSelection();

				if (tabbedPane.getSelectedComponent() != null && buttonPanel != null) {
					if (tabbedPane.getSelectedComponent().equals(mails))
						buttonPanel.enableAddButton(false);
					else
						buttonPanel.enableAddButton(true);
				}

			}
		});

		devices = new DevicesMainPanel(Tabs.devices);
		devices.getTable().addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (devices.getSelected() > -1) {
					buttonPanel.enableDelete();

				} else {
					buttonPanel.enableButtons(false);
				}

			}
		});

		mails = new MailPanel(Tabs.mails);
		mails.getTable().addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (mails.getSelected() > -1) {
					buttonPanel.enableButtons(false);
					buttonPanel.enableShow();

				} else {
					buttonPanel.enableButtons(false);
				}

			}
		});

		all = new MainPanel(Tabs.all);
		all.setModel(contractorDAO.getAll());
		all.getTable().addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (all.getSelected() > -1) {
					buttonPanel.enableButtons(true);
					buttonPanel.setMailEnabled(all.isSelectedEmail());
				} else {
					buttonPanel.enableButtons(false);
				}

			}
		});
		projectants = new MainPanel(Tabs.projectants);
		projectants.setModel(contractorDAO.getAllOfType(Tabs.projectants));
		projectants.getTable().addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (projectants.getSelected() > -1) {
					buttonPanel.enableButtons(true);
					buttonPanel.setMailEnabled(projectants.isSelectedEmail());
				} else {
					buttonPanel.enableButtons(false);
				}

			}
		});
		architects = new MainPanel(Tabs.architects);
		architects.setModel(contractorDAO.getAllOfType(Tabs.architects));
		architects.getTable().addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (architects.getSelected() > -1) {
					buttonPanel.enableButtons(true);
					buttonPanel.setMailEnabled(architects.isSelectedEmail());
				} else {
					buttonPanel.enableButtons(false);
				}
			}
		});
		services = new MainPanel(Tabs.services);
		services.setModel(contractorDAO.getAllOfType(Tabs.services));
		services.getTable().addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (services.getSelected() > -1) {
					buttonPanel.enableButtons(true);
					buttonPanel.setMailEnabled(services.isSelectedEmail());
				} else {
					buttonPanel.enableButtons(false);
				}

			}
		});

		tabbedPane.addTab(Tabs.all, all);
		tabbedPane.addTab(Tabs.projectants, projectants);
		tabbedPane.addTab(Tabs.architects, architects);
		tabbedPane.addTab(Tabs.services, services);
		tabbedPane.addTab(Tabs.devices, devices);
		tabbedPane.addTab(Tabs.mails, mails);

		add(tabbedPane, BorderLayout.CENTER);

		buttonPanel = new BottomPanel(tabbedPane);
		add(buttonPanel, BorderLayout.SOUTH);

		setVisible(true);

	}
}
